package com.ds.robot.assistant.util;

import lombok.extern.slf4j.Slf4j;

import java.time.DayOfWeek;
import java.time.LocalDate;
import java.time.ZoneId;
import java.time.temporal.TemporalAdjusters;
import java.util.Date;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@Slf4j
public class DateUtil {

    public static Date getYesterday() {
        LocalDate now = LocalDate.now().plusDays(-1);
        return Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }

    /**
     * 获取指定天数前的日期
     *
     * @param day 指定天数
     * @return 返回指定天数前的日期，类型为java.util.Date
     */
    public static Date getDayBefore(int day) {
        LocalDate now = LocalDate.now().plusDays(-day);
        return Date.from(now.atStartOfDay(ZoneId.systemDefault()).toInstant());
    }


    public static void main(String[] args) {
        // LocalDate date = LocalDate.now();
        LocalDate date = LocalDate.parse("2024-10-06");
        System.out.println(date + "是: " + getDayOfWeek(date));

        if (2 > 1) return;

        System.out.println("LocalDate.now() = " + LocalDate.now().toString());


        // 示例日期，你可以替换为任何需要检查的日期
        LocalDate dateToCheck = LocalDate.of(2024, 7, 5); // 假设我们要检查2023年4月1日
        LocalDate dateToCheck2 = LocalDate.of(2024, 5, 4); // 假设我们要检查2023年4月1日
        LocalDate dateToCheck3 = LocalDate.of(2024, 4, 6); // 假设我们要检查2023年4月1日

        // 判断给定日期是否为本月第一个星期六
        //boolean isFirstSaturdayOfMonth = isFirstSaturday(dateToCheck);
        boolean isFirstSaturdayOfMonth = isFirstSaturdayOfMonth(dateToCheck);
        boolean isFirstSaturdayOfMonth2 = isFirstSaturdayOfMonth(dateToCheck2);
        boolean isFirstSaturdayOfMonth3 = isFirstSaturdayOfMonth(dateToCheck3);

        System.out.println("isFirstSaturdayOfMonth = " + isFirstSaturdayOfMonth);
        System.out.println("isFirstSaturdayOfMonth2 = " + isFirstSaturdayOfMonth2);
        System.out.println("isFirstSaturdayOfMonth3 = " + isFirstSaturdayOfMonth3);

        if (isFirstSaturdayOfMonth) {
            System.out.println(dateToCheck + " 是本月的第一个星期六。");
        } else {
            System.out.println(dateToCheck + " 不是本月的第一个星期六。");
        }
    }

    /**
     * 判断给定的日期是否为所在月份的第一个星期六。
     *
     * @param date LocalDate对象，待检查的日期
     * @return 如果是第一个星期六返回true，否则返回false
     */
    public static boolean isFirstSaturdayOfMonth(LocalDate date) {
        // 获取当前月份的第一天
        LocalDate firstDayOfMonth = date.withDayOfMonth(1);
        //LocalDate firstDayOfMonth2 = date.with(TemporalAdjusters.firstDayOfMonth());
        log.debug("firstDayOfMonth = {}" , firstDayOfMonth);

        // 找到第一个星期六
        LocalDate firstSaturday = firstDayOfMonth.with(TemporalAdjusters.nextOrSame(DayOfWeek.SATURDAY));

        // 比较给定的日期是否是第一个星期六
        return date.equals(firstSaturday);
    }

    public static String getDayOfWeek(LocalDate date) {
        DayOfWeek dayOfWeek = date.getDayOfWeek();
        // 将ISO标准的DayOfWeek转换为中国的DayOfWeek
        // DayOfWeek chinaDayOfWeek = DayOfWeek.of((dayOfWeek.getValue() % 7) + 1);
        switch (dayOfWeek) {
            case MONDAY:
                return "星期一";
            case TUESDAY:
                return "星期二";
            case WEDNESDAY:
                return "星期三";
            case THURSDAY:
                return "星期四";
            case FRIDAY:
                return "星期五";
            case SATURDAY:
                return "星期六";
            case SUNDAY:
                return "星期日";
            default:
                throw new IllegalArgumentException("非法参数");
        }
    }


}
